#ifndef AVALON_TCPSOCKETWRITER_HPP
#define AVALON_TCPSOCKETWRITER_HPP
#include <boost/enable_shared_from_this.hpp>
#include "Avalon/IO/Buffer.hpp"
#include "Avalon/IO/Writer.hpp"
#include "Avalon/Network/Network.hpp"

namespace boost {
namespace system {
  class error_code;
}
}

namespace Avalon {
namespace Network {

  /*! \class TcpSocketWriter
   *  \brief Writes to a socket.
   */
  class TcpSocketWriter : public IO::Writer,
      public boost::enable_shared_from_this<TcpSocketWriter> {
    public:

      virtual ~TcpSocketWriter();

      virtual void Write(const void* data, int size,
        Out<Threading::Async<void> > result);

      virtual void Write(const IO::Buffer& data,
        Out<Threading::Async<void> > result);

    private:
      friend class TcpServerSocket;
      friend class TcpClientSocketChannel;
      friend class TcpSocketConnection;
      boost::shared_ptr<Detail::SocketType> m_socket;
      IO::Buffer m_data;
      Threading::Async<void>* m_result;
      boost::function<void (const boost::system::error_code&, std::size_t)>
        m_writeSlot;

      TcpSocketWriter(const boost::shared_ptr<Detail::SocketType>& socket);
      void OnWrite(const boost::system::error_code& error, std::size_t size);
  };
}
}

#endif // AVALON_TCPSOCKETWRITER_HPP
